<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择所属项目" clearable filterable class="product-style">
          <el-option v-for="dict in projectOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属公司" prop="transportCompanyId">
        <el-select v-model="queryParams.transportCompanyId" placeholder="请选择所属公司" clearable filterable>
          <el-option v-for="dict in transportOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input v-model="queryParams.driverName" placeholder="请输入司机姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="运输状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择运输状态" clearable>
          <el-option v-for="dict in dict.type.transport_state" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="运输时间">
        <el-date-picker v-model="daterangeBeginTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['orderManage:transport:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['orderManage:transport:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['orderManage:transport:remove']">批量删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['orderManage:transport:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属项目" align="center" prop="projectName" width="200" show-overflow-tooltip />
      <el-table-column label="所属公司" align="center" prop="transportCompanyName" width="200" show-overflow-tooltip />
      <el-table-column label="资源利用化企业" align="center" prop="disposalLandName" width="200" show-overflow-tooltip />
      <el-table-column label="车牌号" align="center" prop="carNumber" width="100" show-overflow-tooltip />
      <el-table-column label="车辆类型" align="center" prop="carType" width="100" show-overflow-tooltip />
      <el-table-column label="司机姓名" align="center" prop="driverName" width="100" show-overflow-tooltip />
      <el-table-column label="司机电话" align="center" prop="driverPhone" width="100" show-overflow-tooltip />
      <el-table-column label="车辆载重(吨)" align="center" prop="vehicleLoad" />
      <el-table-column label="开始时间" align="center" prop="beginTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始图片" align="center" prop="beginImage">
        <template slot-scope="scope">
          <image-preview :src="scope.row.beginImage" :width=50 :height=50 />
        </template>
      </el-table-column>
      <el-table-column label="开始位置" align="center" prop="beginLocation">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleLocation(scope.row.beginLocation)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="开始位置是否超出范围" align="center" prop="beginLocationExceed">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_valid" :value="scope.row.beginLocationExceed" />
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束图片" align="center" prop="endImage">
        <template slot-scope="scope">
          <image-preview :src="scope.row.endImage" :width=50 :height=50 />
        </template>
      </el-table-column>
      <el-table-column label="结束位置" align="center" prop="endLocation">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleLocation(scope.row.endLocation)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="结束位置是否超出范围" align="center" prop="endLocationExceed">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_valid" :value="scope.row.endLocationExceed" />
        </template>
      </el-table-column>
      <el-table-column label="运输状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.transport_state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['orderManage:transport:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['orderManage:transport:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改运输对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车牌号" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车辆类型" prop="carType">
          <el-select v-model="form.carType" placeholder="请选择车辆类型">
            <el-option v-for="dict in dict.type.car_type_id" :key="dict.value" :label="dict.label" :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机姓名" />
        </el-form-item>
        <el-form-item label="司机电话" prop="driverPhone">
          <el-input v-model="form.driverPhone" placeholder="请输入司机电话" />
        </el-form-item>
        <el-form-item label="车辆载重(吨)" prop="vehicleLoad">
          <el-input v-model="form.vehicleLoad" placeholder="请输入车辆载重(吨)" />
        </el-form-item>
        <el-form-item label="开始时间" prop="beginTime">
          <el-date-picker clearable v-model="form.beginTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始位置是否超出范围" prop="beginLocationExceed">
          <el-select v-model="form.beginLocationExceed" placeholder="请选择开始位置是否超出范围">
            <el-option v-for="dict in dict.type.sys_valid" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束位置是否超出范围" prop="endLocationExceed">
          <el-input v-model="form.endLocationExceed" placeholder="请输入结束位置是否超出范围" />
        </el-form-item>
        <el-form-item label="运输状态( 10:进行中;20:已完成 )" prop="state">
          <el-select v-model="form.state" placeholder="请选择运输状态( 10:进行中;20:已完成 )">
            <el-option v-for="dict in dict.type.booking_state" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属司机( 司机 )" prop="driverId">
          <el-input v-model="form.driverId" placeholder="请输入所属司机( 司机 )" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--地图显示-->
    <el-dialog :visible.sync="mapOpen" fullscreen append-to-body>
      <TMap :longitude="longitude" :latitude="latitude" :address="address" tid="tmap_transport" />
    </el-dialog>
  </div>
</template>

<script>
import { listTransport, getTransport, delTransport, addTransport, updateTransport } from "@/api/orderManage/transport";
import { allCompany } from "@/api/company/company";
import { allProject } from "@/api/projectManage/project";
import { allDisposalLand } from "@/api/disposal/disposalLand";
import TMap from '@/components/Tmap/TMap';
import TFMap from '@/components/Tmap/TFMap';

export default {
  name: "Transport",
  dicts: ['car_type_id', 'transport_state', 'sys_valid'],
  components: { TMap, TFMap },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运输表格数据
      transportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        bookingId: null,
        carNumber: null,
        carType: null,
        driverName: null,
        driverPhone: null,
        vehicleLoad: null,
        beginTime: null,
        beginLocation: null,
        beginLocationExceed: null,
        endTime: null,
        endLocation: null,
        endLocationExceed: null,
        state: null,
        driverId: null,
        disposalLandId: null,
        projectId: null,
        transportCompanyId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        beginTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
      },
      // 清运公司选项
      transportOptions: [],
      // 项目选项
      projectOptions: [],
      // 资源利用化企业选项
      disposalLandOptions: [],
      // 是否地图显示弹出层
      mapOpen: false,
      // 项目坐标
      position: {},
      longitude: 118.83414506997173,
      latitude: 31.954718562357378,
      address: '江宁区政府',
      daterangeBeginTime: [],
    };
  },
  created () {
    this.getList();
    this.getTransportCompanyList();
    this.getProjectList();
    this.getDisposalLandList();
  },
  methods: {
    // 清运公司列表
    getTransportCompanyList () {
      allCompany().then(response => {
        this.transportOptions = response.data;
      })
    },
    // 项目列表
    getProjectList () {
      allProject().then(response => {
        this.projectOptions = response.data;
      })
    },
    // 资源利用化企业选项列表
    getDisposalLandList () {
      allDisposalLand().then(response => {
        this.disposalLandOptions = response.data;
      })
    },
    /** 查询运输列表 */
    getList () {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeBeginTime && '' != this.daterangeBeginTime) {
        this.queryParams.params["beginBeginTime"] = this.daterangeBeginTime[0];
        this.queryParams.params["endBeginTime"] = this.daterangeBeginTime[1];
      }
      listTransport(this.queryParams).then(response => {
        this.transportList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        bookingId: null,
        carNumber: null,
        carType: null,
        driverName: null,
        driverPhone: null,
        vehicleLoad: null,
        beginTime: null,
        beginLocation: null,
        beginLocationExceed: null,
        endTime: null,
        endLocation: null,
        endLocationExceed: null,
        state: null,
        driverId: null,
        disposalLandId: null,
        projectId: null,
        transportCompanyId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeBeginTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加运输";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getTransport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运输";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTransport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delTransport(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('orderManage/transport/export', {
        ...this.queryParams
      }, `运输单_${new Date().getTime()}.xlsx`)
    },
    // 定位
    handleLocation (location) {
      if (location && location.coordinates) {
        this.longitude = location.coordinates[0];
        this.latitude = location.coordinates[1];
        this.address = '';
        this.mapOpen = true;
      } else {
        this.$modal.msgWarning("没有坐标数据");
      }
    },
  }
};
</script>

<style scoped>
.product-style /deep/ .el-select-dropdown__item {
  width: 300px;
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>